clear all
global system "linux"

if "${system}" == "linux" {
	*global code "/"
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"


**** Freddie Mac Performance data
use "./embs-freddie-jm-hist.dta", clear // extract Freddie loans' monthly balance history  from EMBS loan
sort loanseqnum_id factor_date
by loanseqnum_id: gen lastdt = factor_date[_N]
format lastd %td

qui su factor_date
gen prepaid = lastdt < `r(max)' 
gen max_dt = `r(max)'
format max_dt %td

// merge with static data to calcualte loan age
merge 1:1 loanseqnum_id factor_date using "${cached}/embs-freddie-cleaned.dta", nogen keep(master matched) keepusing(loanage)
sort loanseqnum_id factor_date
by loanseqnum_id: replace loanage = loanage[_n-1]+1 if _n>1

// keep only a certin loanage 
by loanseqnum_id: gen last = _n == _N
keep if inlist(loanage, 12, 24, 36, 48, 60, 72, 84, 96)==1 | last==1


// Reshape the data
sort loanseqnum_id factor_date
by loanseqnum_id: gen last_age = loanage[_N]

keep if inlist(loanage, 12, 24, 36, 48, 60, 72, 84, 96)
keep loanseqnum_id curr_rpb last_age loanage prepaid max_dt
reshape wide curr_rpb, i(loanseqnum_id) j(loanage)

save "${cached}/embs-freddie-performance-wide.dta", replace


**** Combine both data 
use "${cached}/embs-fannie-cleaned.dta", clear

gen agency_id = 1 
tostring loanseqnum_id, replace

append using "${cached}/embs-freddie-cleaned.dta"
replace agency_id = 2 if agency_id == .

label de agency_id 1 "Fannie" 2 "Freddie", replace
label val agency_id agency_id

keep if year(poolissue_date) >= 2008 


// keep only 30-year embs_products
keep if regexm(embs_product, "FHLG30") | regexm(embs_product, "FNM30")

compress


save "${cached}/embs-GSE-loanlevel-combined.dta", replace



